﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HomeController.cs" company="Oliver Kurowski">
//    (c) Oliver Kurowski. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace RegioKistl.Controllers
{
    using System.Linq;
    using System.Web.Mvc;

    using RegioKistl.Models.DataAccess;

    /// <summary>
    /// Controller for the Home page.
    /// </summary>
    public class HomeController : Controller
    {
        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var data = new Models.UserStartPageData();

            if (this.User.Identity.IsAuthenticated)
            {
                using (var session = Repository.OpenSession())
                {
                    // load the customer data associated with the logged in user.
                    var customerData = session.Query<Models.Customer>().FirstOrDefault(x => x.AccountName == this.User.Identity.Name);

                    data.CustomerData = customerData ?? new Models.Customer();

                    // load the latest deliveries
                    if (customerData != null)
                    {
                        var deliveries = from delivery in session.Query<Models.Delivery>()
                                         where delivery.CustomerId == customerData.Id
                                         orderby delivery.DeliveryDate descending
                                         select delivery;

                        data.LastestDeliveries = deliveries.Take(10).ToList();
                    }

                    // Just for development...
                    data.Message = "Welcome " + this.User.Identity.Name + "!";
                }
            }
            else
            {
                data.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            }

            return View(data);
        }

        /// <summary>
        /// Abouts this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        /// <summary>
        /// Contacts this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateUser()
        {
            return this.View();
        }
    }
}
